rm(list = ls())
load("dataBJPOLS.RData")

tests2 <- function(name, x1, y1) {
    t1 <- t.test(x1[y1 == "B"], x1[y1 == "W"], alternative = "two.sided", var.equal = FALSE)
    t2 <- t.test(x1[y1 == "B"], x1[y1 == "H"], alternative = "two.sided", var.equal = FALSE)
    t3 <- t.test(x1[y1 == "H"], x1[y1 == "W"], alternative = "two.sided", var.equal = FALSE)
    
    x <- data.table(round(c(t1$estimate[1] - t1$estimate[2], t1$p.value), 2), 
                    round(c(t2$estimate[1] - t2$estimate[2], t2$p.value), 2),
                    round(c(t3$estimate[1] - t3$estimate[2], t3$p.value), 2))
    x$V4 <- c(paste("difference in", name), "p-value")
    colnames(x) <- c("Black-White",
                     "Black-Hispanic",
                     "Hispanic-White",
                     "Variable")
    return(x)
}

t1 <- rbind(tests2(name = "PTI", x1 = last.cases$pti, y1 = last.cases$judge_race),
            tests2(name = "Age", x1 = last.cases$age, y1 = last.cases$judge_race),
            tests2(name = "Black defendant", x1 = last.cases$race4 == "B", y1 = last.cases$judge_race),
            tests2(name = "White defendant", x1 = last.cases$race4 == "W", y1 = last.cases$judge_race),
            tests2(name = "Hispa defendant", x1 = last.cases$race4 == "H", y1 = last.cases$judge_race),
            tests2(name = "Female", x1 = last.cases$female, y1 = last.cases$judge_race),
            tests2(name = "Prev. Turnout", x1 = last.cases$vote_pre, y1 = last.cases$judge_race),
            tests2(name = "Turnout", x1 = last.cases$vote_post, y1 = last.cases$judge_race),
            tests2(name = "Not Eligible", x1 = last.cases$noteli, y1 = last.cases$judge_race),
            tests2(name = "Registration", x1 = last.cases$regis_before, y1 = last.cases$judge_race),
            tests2(name = "Severity", x1 = last.cases$severity, y1 = last.cases$judge_race),
            tests2(name = "Any Drug", x1 = last.cases$any_drug, y1 = last.cases$judge_race),
            tests2(name = "Any Weapon", x1 = last.cases$any_weapon, y1 = last.cases$judge_race),
            tests2(name = "Any Property", x1 = last.cases$any_prop, y1 = last.cases$judge_race),
            tests2(name = "Any Prior", x1 = last.cases$any_prior_case, y1 = last.cases$judge_race),
            tests2(name = "Number of Days", x1 = last.cases$daysjail_t, y1 = last.cases$judge_race))
t1 <- t1[, c(4, 1:3)]
t1

tests3 <- function(name, x1, y1, y2) {
    t1 <- t.test(x1[y1 == "B" & y2 == "B"], x1[y1 == "W" & y2 == "B"], alternative = "two.sided", var.equal = FALSE)
    t2 <- t.test(x1[y1 == "B" & y2 == "B"], x1[y1 == "H" & y2 == "B"], alternative = "two.sided", var.equal = FALSE)
    t3 <- t.test(x1[y1 == "H" & y2 == "B"], x1[y1 == "W" & y2 == "B"], alternative = "two.sided", var.equal = FALSE)
    
    x <- data.table(round(c(t1$estimate[1] - t1$estimate[2], t1$p.value), 2), 
                    round(c(t2$estimate[1] - t2$estimate[2], t2$p.value), 2),
                    round(c(t3$estimate[1] - t3$estimate[2], t3$p.value), 2))
    x$V4 <- c(paste("difference in", name), "p-value")
    colnames(x) <- c("Black-White Judge (Black Defendants)",
                     "Black-Hispanic Judge (Black Defendants)",
                     "Hispanic-White Judge (Black Defendants)",
                     "Variable")
    return(x)
}

t2 <- rbind(tests3(name = "PTI", x1 = last.cases$pti, y1 = last.cases$judge_race, y2 = last.cases$race4),
            tests3(name = "Age", x1 = last.cases$age, y1 = last.cases$judge_race, y2 = last.cases$race4),
            tests3(name = "Female", x1 = last.cases$female, y1 = last.cases$judge_race, y2 = last.cases$race4),
            tests3(name = "Prev. Turnout", x1 = last.cases$vote_pre, y1 = last.cases$judge_race, y2 = last.cases$race4),
            tests3(name = "Turnout", x1 = last.cases$vote_post, y1 = last.cases$judge_race, y2 = last.cases$race4),
            tests3(name = "Not Eligible", x1 = last.cases$noteli, y1 = last.cases$judge_race, y2 = last.cases$race4),
            tests3(name = "Registration", x1 = last.cases$regis_before, y1 = last.cases$judge_race, y2 = last.cases$race4),
            tests3(name = "Severity", x1 = last.cases$severity, y1 = last.cases$judge_race, y2 = last.cases$race4),            
            tests3(name = "Any Drug", x1 = last.cases$any_drug, y1 = last.cases$judge_race, y2 = last.cases$race4),
            tests3(name = "Any Weapon", x1 = last.cases$any_weapon, y1 = last.cases$judge_race, y2 = last.cases$race4),
            tests3(name = "Any Property", x1 = last.cases$any_prop, y1 = last.cases$judge_race, y2 = last.cases$race4),
            tests3(name = "Any Prior", x1 = last.cases$any_prior_case, y1 = last.cases$judge_race, y2 = last.cases$race4),
            tests3(name = "Number of Days", x1 = last.cases$daysjail_t, y1 = last.cases$judge_race, y2 = last.cases$race4))
t2 <- t2[, c(4, 1:3)]
t2

